using System;
using MathService.Functions.PSO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MathServiceTestProject
{
    [TestClass]
    public class PSOOptimizator_Test
    {
        private const int SamplesCount = 100;

        private static readonly Random sf_RND = new Random();
        private static double DNR(double M = 0, double D = 1) { return (sf_RND.NextDouble() - 0.5) * D + M; }

        public TestContext TestContext { get; set; }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        private static double Parabola(double x) { return x * x; }


        [TestMethod, Priority(0)]
        public void FunctionMinimizerTest()
        {
            const double Dx = 10.0;
            for(var i = 0; i < SamplesCount; i++)
            {
                Func<double, double> f = Parabola;
                var x0 = DNR(Dx, Dx);
                var y0 = DNR(Dx, Dx);
                f = f.ArgumentShift(x0);
                f = f.Add(y0);

                double x;
                double y;
                f.Minimize(x0 - Dx / 2, x0 + Dx / 2, 100, out x, out y);

                Assert.AreEqual(x0, x, 0.448, string.Format("x[{0}], delta = {1}", i, x0 - x));
                Assert.AreEqual(y0, y, 0.189, string.Format("y[{0}], delta = {1}", i, y0 - y));
            }
        }

        [TestMethod, Priority(0)]
        public void FunctionMaximizerTest()
        {
            const double Dx = 10.0;
            for(var i = 0; i < SamplesCount; i++)
            {
                Func<double, double> f = Parabola;
                var x0 = DNR(Dx, Dx);
                var y0 = DNR(Dx, Dx);
                f = f.Reverse();
                f = f.ArgumentShift(x0);
                f = f.Add(y0);

                double x;
                double y;
                f.Maximize(x0 - Dx / 2, x0 + Dx / 2, 100, out x, out y);

                Assert.AreEqual(x0, x, 0.49, string.Format("x[{0}], delta = {1}", i, x0 - x));
                Assert.AreEqual(y0, y, 0.178, string.Format("y[{0}], delta = {1}", i, y0 - y));
            }
        }
    }
}